#ifndef MANAGER_H
#define MANAGER_H
#include "gameroom.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QList>
#include <QMap>
#include <QObject>
#include <QTcpSocket>
#include <mutex>
class QTcpSocket;
class Request;

class Manager : public QObject {
  Q_OBJECT
public:
  static Manager *instance() {
    static Manager manager;
    return &manager;
  }

public:
  explicit Manager(QObject *parent = nullptr);
  virtual ~Manager();

  void addClient(QTcpSocket *client);

  void addRoom(GameRoom *room);
  bool existRoom(const QString &roomName);
  GameRoom *takeRoom(const QString &roomName);
  GameRoom *room(const QString &roomName);

public Q_SLOTS:
  void disconnectedHandle();
  void readyReadHandle();

private:
  // handles
  bool joinRoomHandler(Request &request, QTcpSocket *client);
  bool playGameHandler(Request &request);
  bool leaveHandler(Request &request);
  bool voteHandler(Request &request);
  bool talkHandler(Request &request);

private:
  void responseSuccess();
  void responseError();
signals:

public:
  QMap<QString, GameRoom *> m_rooms;
  QList<QTcpSocket *> m_clients;
};

#endif // MANAGER_H
